package com.example.administrator.funchat.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.funchat.Contans.Config;
import com.example.administrator.funchat.R;
import com.example.administrator.funchat.model.User;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by zxtzx on 2016/7/14.
 */
public class FriendInformationActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_information_back;//返回按钮
    private Button btn_add_friend;//添加好友按钮
    private ImageView myself_bg;//背景
    private ImageView head_image;//头像
    private TextView myself_name;//昵称
    private TextView xinqing;//个性签名
    private TextView min_name;//昵称
    private TextView sex;//性别
    private TextView address;//地址
    private Button change_button;//修改按钮
    private User friend_user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_information);
        Log.i("add", "onCreate: 跳转过来");
        initView();//初始化控件

        friend_user = (User) getIntent().getSerializableExtra("info");//得到好友信息
        Log.i("test", "onCreate: ==" + friend_user.getPhone());
        resetInfo(friend_user);//设置好友信息
    }

    private void initView() {
        tv_information_back = (TextView) findViewById(R.id.tv_information_back);
        tv_information_back.setVisibility(View.VISIBLE);//显示返回按钮
        tv_information_back.setOnClickListener(this);
        change_button = (Button) findViewById(R.id.change_button);
        change_button.setVisibility(View.INVISIBLE);//隐藏修改按钮

        btn_add_friend = (Button) findViewById(R.id.btn_add_friend);
        btn_add_friend.setOnClickListener(this);

        myself_bg = (ImageView) findViewById(R.id.myself_bg);
        head_image = (ImageView) findViewById(R.id.head_image);
        myself_name = (TextView) findViewById(R.id.myself_name);
        xinqing = (TextView) findViewById(R.id.xinqing);
        min_name = (TextView) findViewById(R.id.min_name);
        sex = (TextView) findViewById(R.id.sex);
        address = (TextView) findViewById(R.id.address);


    }

    public void resetInfo(User user) {
        x.image().bind(head_image, user.getHeadImg());
        myself_name.setText(user.getName());
        xinqing.setText(user.getSignature());
        min_name.setText(user.getName());
        sex.setText(user.getSex());
        address.setText(user.getAddress());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_information_back:
                finish();
                break;
            case R.id.btn_add_friend:

                RequestParams params = new RequestParams(Config.ADD_FRIEND);
                params.addBodyParameter("mine_phone", MainActivity.u.getPhone());
                params.addBodyParameter("friend_phone", friend_user.getPhone());
                x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        Log.i("add", "onSuccess: 添加好友请求返回值==" + s);
                        if (s.equals("ok")) {
                            Toast.makeText(FriendInformationActivity.this, "已发送请求", Toast.LENGTH_SHORT).show();
                        } else if (s.equals("error")) {
                            Toast.makeText(FriendInformationActivity.this, "发送请求失败", Toast.LENGTH_SHORT).show();
                        } else if (s.equals("existe")) {
                            Toast.makeText(FriendInformationActivity.this, "你们已经是好友了！", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(FriendInformationActivity.this, "服务器异常", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Throwable throwable, boolean b) {

                    }

                    @Override
                    public void onCancelled(CancelledException e) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });

                break;

        }
    }
}
